using UnityEngine;
using UnityEditor;
using System;


namespace UMPRelease
{
    public class UMPReleaseEditor
    {
        public static string DefaultPath = "Assets/Editor/UMPRelease.asset";
        [MenuItem("UMP Release/Init Data Handle")]
        public static void InitVersionData()
        {
            var data = AssetDatabase.LoadAssetAtPath<VersionHandle>(UMPReleaseEditor.DefaultPath);
            if (data == null)
            {
                data = ScriptableObject.CreateInstance<UMPRelease.VersionHandle>();
                AssetDatabase.CreateAsset(data, DefaultPath);
                AssetDatabase.Refresh();
            }
        }

        [MenuItem("UMP Release/Show Dialog")]
        public static void ShowDialog()
        {
            EditorInputDialog.Show("版本发布", "");
        }
    }


    // 简单的输入对话框类
    public class EditorInputDialog : EditorWindow
    {
        private string inputText = "";
        private string title = "";
        private System.Action<string> onOk;
        private System.Action onCancel;

        private VersionHandle data;

        public static string Show(string title, string defaultValue = "")
        {
            EditorInputDialog window = CreateInstance<EditorInputDialog>();
            window.title = title;
            window.inputText = defaultValue;
            window.position = new Rect(Screen.width / 2 - 200, Screen.height / 2 - 50, 400, 300);

            window.data = AssetDatabase.LoadAssetAtPath<VersionHandle>(UMPReleaseEditor.DefaultPath);
            window.data.Refresh();

            window.ShowModal();

            return window.inputText;
        }

        void OnGUI()
        {
            if (data != null)
            {
                GUILayout.BeginVertical();

                // change log
                GUILayout.BeginHorizontal();
                // GUILayout.Label("name", EditorStyles.wordWrappedLabel);
                data.ChangeLog = EditorGUILayout.ObjectField("ChangeLog", data.ChangeLog, typeof(TextAsset), false) as TextAsset;
                GUILayout.EndHorizontal();

                // name
                GUILayout.BeginHorizontal();
                GUILayout.Label("name", EditorStyles.wordWrappedLabel);
                data.PackageConfig.name = EditorGUILayout.TextField(data.PackageConfig.name);
                GUILayout.EndHorizontal();

                // displayName
                GUILayout.BeginHorizontal();
                GUILayout.Label("displayName", EditorStyles.wordWrappedLabel);
                data.PackageConfig.displayName = EditorGUILayout.TextField(data.PackageConfig.displayName);
                GUILayout.EndHorizontal();

                // version
                GUILayout.BeginHorizontal();
                GUILayout.Label("version", EditorStyles.wordWrappedLabel);
                data.PackageConfig.version = EditorGUILayout.TextField(data.PackageConfig.version);
                GUILayout.EndHorizontal();

                // unity version
                GUILayout.BeginHorizontal();
                GUILayout.Label("unity version", EditorStyles.wordWrappedLabel);
                data.PackageConfig.unity = EditorGUILayout.TextField(data.PackageConfig.unity);
                GUILayout.EndHorizontal();

                // unity release version
                GUILayout.BeginHorizontal();
                GUILayout.Label("relase version", EditorStyles.wordWrappedLabel);
                data.PackageConfig.unityRelease = EditorGUILayout.TextField(data.PackageConfig.unityRelease);
                GUILayout.EndHorizontal();

                GUILayout.EndVertical();
            }

            if (GUILayout.Button("确定"))
            {
                GitSyncAPI.SyncBranchAndTag(data.SyncGit.remoteName,
                data.SyncGit.sourceBranch,
                data.SyncGit.targetBranch,
                data.SyncGit.tagName,
                data.PackageConfig.name,
                data.SyncGit.commitMessage);
                // Close();
            }
        }
    }

    [Serializable]
    public class PackageData
    {
        public string name;
        public string displayName;
        public string version;

        public string unity;

        public string unityRelease;

        public string category;

        public object dependencies;
    }

    [Serializable]
    public class GitHandleData
    {
        public string remoteName = "origin";
        public string sourceBranch = "master";
        public string targetBranch = "upm";
        public string tagName = "v1.0.0";
        public string commitMessage = "Release v1.0.0";
    }
}

